SuperStrict

Framework sedm.bo3d
Import maxgui.drivers
Import brl.eventqueue
Import brl.d3d9max2d
Import brl.timer

AddHook(EmitEventHook,TColorPicker.EventHandler,Null,1)

Type TColorPicker Extends TProxyGadget
	
	Global list:TList=New TList
	
	Field panel:TGadget
	Field textfield:TGadget
	Field button:TGadget
	Field r:Int,g:Int,b:Int
	Field link:TLink
	
	Method CleanUp()
		link.remove()
		Super.CleanUp()
	EndMethod
	
	Function Create:TColorPicker(x:Int,y:Int,width:Int,height:Int,group:TGadget)
		Local colorpicker:TColorPicker=New TColorPicker
		colorpicker.panel=CreatePanel(x,y,width,height,group)
		colorpicker.setproxy(colorpicker.panel)
		colorpicker.textfield=CreateTextField(0,0,colorpicker.panel.ClientWidth()-colorpicker.panel.ClientHeight(),colorpicker.panel.ClientHeight(),colorpicker.panel)
		colorpicker.button=CreateButton("",colorpicker.panel.ClientWidth()-colorpicker.panel.ClientHeight(),0,colorpicker.panel.ClientHeight(),colorpicker.panel.ClientHeight(),colorpicker.panel)
		SetGadgetLayout colorpicker.textfield,1,1,1,1
		SetGadgetLayout colorpicker.button,0,1,1,1
		SetGadgetText colorpicker.textfield,"255,255,255"
		SetGadgetColor colorpicker.button,255,255,255
		SetGadgetFilter colorpicker.textfield,filter
		colorpicker.link=list.addlast(colorpicker)
		Return colorpicker
	EndFunction
	
	Function Filter:Int(event:TEvent,context:Object)
		If event.ID=EVENT_KEYCHAR
			If event.data=KEY_BACKSPACE Return 1
			If event.data=KEY_COMMA Return 1
			If event.data<48 Or event.data>57 Return 0
		EndIf
		Return 1
	EndFunction
	
	Method SetColor(r:Int,g:Int,b:Int)
		Self.r=r
		Self.g=g
		Self.b=b
		SetGadgetText textfield,r+","+g+","+b
		SetGadgetColor button,r,g,b
	EndMethod
	
	Method EventHook:TEvent(event:TEvent)
		If event.id=EVENT_GADGETLOSTFOCUS And event.source=textfield
			Local sarr:String[]=GadgetText(textfield).split(",")
			If sarr.length=3
				SetColor Int(sarr[0]),Int(sarr[1]),Int(sarr[2])
			Else
				SetColor r,g,b
			EndIf
			Return CreateEvent(EVENT_GADGETACTION,Self,b+(g Shl 8)+(r Shl 16)+(255 Shl 24))
		EndIf
		If event.id=EVENT_GADGETACTION And event.source=button
			If RequestColor(r,g,b)
				SetColor(RequestedRed(),RequestedGreen(),RequestedBlue())
				Return CreateEvent(EVENT_GADGETACTION,Self,b+(g Shl 8)+(r Shl 16)+(255 Shl 24))
			Else
				Return Null
			EndIf
		EndIf
		Return null
	EndMethod
	
	Function EventHandler:Object(id:Int,data:Object,context:Object)
		If list.isempty() Return data
		Local event:TEvent
		Local colorpicker:TColorPicker
		event=TEvent(data)
		If event
			For colorpicker=EachIn list
				If colorpicker.button=event.source Or colorpicker.textfield=event.source
					Return colorpicker.EventHook(event)
				EndIf
			Next
		EndIf
		Return data
	EndFunction
	
EndType

Function CreateColorPicker:TColorPicker(x:Int,y:Int,width:Int,height:Int,group:TGadget)
	Return TColorPicker.Create(x,y,width,height,group)
EndFunction


Local desk:TGadget = Desktop()

Local win:TGadget = CreateWindow("Ogre3D Canvas", (desk.GetWidth()/2)-400, (desk.GetHeight()/2)-300, 800, 600, Null, WINDOW_TITLEBAR|WINDOW_STATUS)
Local w:Int = ClientWidth(win)
Local h:Int = ClientHeight(win)

Local lbl:TGadget = CreateLabel("Select Backgroundcolor", 4, 4, 190, 20, win)
Local cp:TGadget = CreateColorPicker(4, 24, 190, 20, win)
SetGadgetLayout cp,1,1,1,0
SetGadgetColor cp,255,0,0

Local canvas:TGadget = CreateCanvas(200, 4, w - 204, h - 8, win)
Local chwnd:Int = QueryGadget(canvas, QUERY_HWND_CLIENT)

' init Ogre3D
Local r:Root = Root.Create()

Local rs:RenderSystem			= r.getRenderSystemByName("Direct3D9 Rendering Subsystem")
r.setRenderSystem(rs)
r.initialise(False)

Local tags:String = ""
tags:+ "externalWindowHandle|" + String(chwnd) + "|"
tags:+ "left|" + String(0) + "|"
tags:+ "top|" + String(0) + "|"

Local owin:RenderWindow = r.createRenderWindow("Ogre3D", ClientWidth(canvas), ClientHeight(canvas), False, tags)

Local Log:LogManager			= New LogManager

Log.logMessage("let's go!")

Local scene:SceneManager		= r.createSceneManager()
Local res:ResourceGroupManager	= New ResourceGroupManager
Local rsn:SceneNode				= scene.getRootSceneNode()
	
Local cam:Camera				= scene.createCamera()
Local vport:Viewport			= owin.addViewPort(cam)
Local ww:Int = vport.getActualWidth()
Local hh:Int = vport.getActualHeight()
cam.setAspectRatio(ww / hh)
cam.setNearClipDistance(0.1)
	
Local piv:SceneNode				= rsn.createChildSceneNode("player")
piv.attachObject(cam)

vport.setBackgroundColour(0.3, 0.5, 0.9)
vport.setCamera(cam)

res.addResourceLocation("media", "FileSystem", "myFiles")
res.initialiseResourceGroup("myFiles")

Local box:Entity				= scene.createEntity("", "box.mesh", "myFiles")
Local cs:SceneNode				= rsn.createChildSceneNode()
	
cs.attachObject(box)

Local timer:TTimer = CreateTimer(60)

Local keycodes:Byte[256]
For Local i:Int = 0 To 255
	keycodes[i] = False
Next

Local buttons:Byte[6]
For Local i:Int = 0 To 5
	buttons[i] = False
Next


Local mx:Int
Local my:Int

Type TMouse
	Field lastX:Int
	Field lastY:Int
	
	Method MouseXSpeed:Int(x:Int)
		Return x - lastX
	End Method
	
	Method MouseYSpeed:Int(y:Int)
		Return y - lastY
	End Method
	
	Method Update(x:Int, y:Int)
		lastx = x
		lasty = y
	End Method
End Type
Local m:TMouse = New TMouse

Repeat
	WaitEvent()
	
	Select EventID()
		Case EVENT_APPTERMINATE, EVENT_WINDOWCLOSE
			Exit
			
		Case EVENT_TIMERTICK			
			If keycodes[KEY_W] Then piv.translate(0.0, 0.0, -1.0, TS_LOCAL)
			If keycodes[KEY_A] Then piv.translate(-1.0, 0.0, 0.0, TS_LOCAL)
			If keycodes[KEY_S] Then piv.translate(0.0, 0.0, 1.0, TS_LOCAL)
			If keycodes[KEY_D] Then piv.translate(1.0, 0.0, 0.0, TS_LOCAL)
			
			If buttons[2]
				cam.pitch(-m.MouseYSpeed(my) * 0.01)
				piv.yaw(-m.MouseXSpeed(mx) * 0.01, TS_LOCAL)
			EndIf
			
			r.renderOneFrame()
			m.Update(mx, my)
			
		Case EVENT_KEYDOWN
			keycodes[EventData()] = True
			
		Case EVENT_KEYUP
			keycodes[EventData()] = False
			
		Case EVENT_MOUSEMOVE
			mx = EventX()
			my = EventY()
						
		Case EVENT_MOUSEDOWN
			buttons[EventData()] = True
						
		Case EVENT_MOUSEUP
			buttons[EventData()] = False
			
		Case EVENT_GADGETACTION
			Select EventSource()
				Case cp
					Local r:Int = RequestedRed()
					Local g:Int = RequestedGreen()
					Local b:Int = RequestedBlue()
					vport.setBackgroundColour((1.0/255.0) * Float(r), (1.0/255.0) * Float(g), (1.0/255.0) * Float(b))
			End Select
	End Select
Forever

r.Shutdown()
End




